package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.*;
import cn.e4j.modular.crm.entity.GwfeT;
import cn.e4j.modular.crm.mapper.GwfeTMapper;
import cn.e4j.modular.crm.service.GwfeTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-10-24
 */
@Service
public class GwfeTServiceImpl extends ServiceImpl<GwfeTMapper, GwfeT> implements GwfeTService {


    @Transactional(readOnly = true)
    @Override
    public List<Dto> getList(Dto inDto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();
        inDto.put("ENT", shiroUser.getDBC());
        inDto.put("LANG", LangUtils.getLang(shiroUser.getLANG()));
        return this.baseMapper.getList(inDto,page);
    }


    @Transactional(readOnly = true)
    @Override
    public Dto getRate(Dto inDto) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ENT", shiroUser.getDBC());
        inDto.put("LANG", LangUtils.getLang(shiroUser.getLANG()));
        if(inDto.getString("CURRENCY").equals("RMB")){
            outDto.put("RATE","1");
            return outDto;
        }
        String docdate = inDto.getString("DOCDATE");
        if(AppCxt.getParam("change_rate_type_").equals( "1")){//日汇率
            try {
                Date docdateNew = new SimpleDateFormat("yyyy-MM-dd").parse(docdate);
                inDto.put("CURRENT_TIME",new SimpleDateFormat("yyyy-MM-dd").format(docdateNew));
                Dto gwelDto = this.baseMapper.getGwel005(inDto);
                if(gwelDto != null && gwelDto.size() > 0){
                    outDto.put("RATE",gwelDto.getString("GWEL005"));
                }else {
                    outDto.put("RATE","N");
                }
            }catch (ParseException e) {
                e.printStackTrace();
            }
        } else {//月汇率
            try {
                Date docdateNew = new SimpleDateFormat("yyyy-MM-dd").parse(docdate);
                inDto.put("CURRENT_TIME",new SimpleDateFormat("yyyy-MM-dd").format(docdateNew));
                Dto gwekDto = this.baseMapper.getGwek005(inDto);
                if(gwekDto != null && gwekDto.size() > 0){
                    outDto.put("RATE",gwekDto.getString("GWEK005"));
                }else {
                    outDto.put("RATE","N");
                }
            }catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Dto> listGwfe(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ENT", shiroUser.getDBC());
        inDto.put("LANG", LangUtils.getLang(shiroUser.getLANG()));
        List<Dto> resultDto = this.baseMapper.listGwfe(inDto);
        return resultDto;
    }

    @Transactional
    @Override
    public void add(List<GwfeT> gwfeTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //增加新的明细
        for (int i = 0; i < gwfeTList.size(); i++) {
            GwfeT gwfeT = gwfeTList.get(i);
            gwfeT.setGWFESEQ(new BigDecimal(i+1));
            gwfeT.setGWFEENT(shiroUser.getDBC());
            gwfeT.setGWFEACTI("N");
            gwfeT.setGWFEUSER(shiroUser.getACCOUNT());
            gwfeT.setGWFEGRUP(shiroUser.getORG_ID());
            gwfeT.setGWFEDATE(LocalDate.now());
            gwfeT.setGWFETIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gwfeT.setGWFEORIG(shiroUser.getORG_ID());
            gwfeT.setGWFEORIU(shiroUser.getACCOUNT());
            gwfeT.insert();
        }
    }

    @Transactional
    @Override
    public void edit(List<GwfeT> gwfeTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //删除旧的
        UpdateWrapper<GwfeT> gwfeTUpdateWrapper=new UpdateWrapper<>();
        gwfeTUpdateWrapper.eq("GWFEENT",shiroUser.getDBC());
        gwfeTUpdateWrapper.eq("GWFEDOCNO",gwfeTList.get(0).getGWFEDOCNO());
        int count = this.baseMapper.delete(gwfeTUpdateWrapper);

        //增加新的
        for (int i = 0; i < gwfeTList.size(); i++) {
            GwfeT gwfeT = gwfeTList.get(i);
            gwfeT.setGWFESEQ(new BigDecimal(i+1));
            gwfeT.setGWFEENT(shiroUser.getDBC());
            gwfeT.setGWFEACTI("N");
            gwfeT.setGWFEUSER(shiroUser.getACCOUNT());
            gwfeT.setGWFEGRUP(shiroUser.getORG_ID());
            gwfeT.setGWFEDATE(LocalDate.now());
            gwfeT.setGWFETIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gwfeT.setGWFEORIG(shiroUser.getORG_ID());
            gwfeT.setGWFEORIU(shiroUser.getACCOUNT());

            gwfeT.insert();
        }
    }

    @Transactional
    @Override
    public void delRow(GwfeT gwfeT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        UpdateWrapper<GwfeT> gwfeTUpdateWrapper = new UpdateWrapper<>();
        gwfeTUpdateWrapper.eq("GWFEENT", shiroUser.getDBC());
        gwfeTUpdateWrapper.eq("GWFEDOCNO",gwfeT.getGWFEDOCNO());
        gwfeT.delete(gwfeTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<GwfeT> gwfeTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        for (GwfeT gwfeT : gwfeTList) {
            UpdateWrapper<GwfeT> gwfeTUpdateWrapper = new UpdateWrapper<>();
            gwfeTUpdateWrapper.eq("GWFEENT", shiroUser.getDBC());
            gwfeTUpdateWrapper.eq("GWFEDOCNO",gwfeT.getGWFEDOCNO());
            gwfeT.delete(gwfeTUpdateWrapper);
        }
    }

    @Transactional
    @Override
    public Dto check(List<GwfeT> gwfeTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        for (GwfeT gwfeT : gwfeTList) {
            UpdateWrapper<GwfeT> gwfeTUpdateWrapper = new UpdateWrapper<>();
            gwfeTUpdateWrapper.eq("GWFEENT", shiroUser.getDBC());
            gwfeTUpdateWrapper.eq("GWFEDOCNO",gwfeT.getGWFEDOCNO());
//            gwfeTUpdateWrapper.eq("GWFESEQ", gwfeT.getGWFESEQ());//去掉项次，整单审核
            gwfeT.setGWFEACTI("Y");
            if(!gwfeT.update(gwfeTUpdateWrapper)){
                outDto.setAppCode(400);
                outDto.setAppMsg("审核失败");
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return outDto;
    }

    @Transactional
    @Override
    public Dto unCheck(List<GwfeT> gwfeTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        for (GwfeT gwfeT : gwfeTList) {
            UpdateWrapper<GwfeT> gwfeTUpdateWrapper = new UpdateWrapper<>();
            gwfeTUpdateWrapper.eq("GWFEENT", shiroUser.getDBC());
            gwfeTUpdateWrapper.eq("GWFEDOCNO",gwfeT.getGWFEDOCNO());
//            gwfeTUpdateWrapper.eq("GWFESEQ", gwfeT.getGWFESEQ());//去掉项次，整单取消审核
            gwfeT.setGWFEACTI("N");
            if(!gwfeT.update(gwfeTUpdateWrapper)){
                outDto.setAppCode(400);
                outDto.setAppMsg("取消审核失败");
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return outDto;
    }

    /**
     *
     * @param gwfe003List gwfe004List
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Dto getCostCheckData(List<GwfeT> gwfe003List, List<GwfeT> gwfe004List, String GWFE001, String GWFE002, String AREAS) {
        ShiroUser shiroUser= ShiroKit.getUser();
        List<Dto> result = new ArrayList<>();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        //区域相关数据查询
        Dto query003Dto = Dtos.newDto();
        query003Dto.put("GWFE003LIST", gwfe003List);
        query003Dto.put("ENT", shiroUser.getDBC());
        query003Dto.put("YEAR", GWFE001);
        query003Dto.put("MONTH", GWFE002);
        Dto lDto = Dtos.newDto();
        AREAS = StringUtils.substringBeforeLast(AREAS,",");
        //年度总预算
        Dto yearTotal1ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal1",query003Dto); //年度
        lDto.put("AREAS",AREAS);
        lDto.put("PROJECT", "年度总预算");
        lDto.put("YEAR_SALES_SUM", yearTotal1ByArea != null ? yearTotal1ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal1ByArea != null ? yearTotal1ByArea.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", "");
        lDto.put("MONTH_EXPENSES_SUM", "");
        result.add(lDto);
        //月累计预算
        Dto yearTotal2ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal2",query003Dto);//年度
        Dto monthTotal2ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal2",query003Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS",AREAS);
        lDto.put("PROJECT", "月累计预算");
        lDto.put("YEAR_SALES_SUM", yearTotal2ByArea != null ? yearTotal2ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal2ByArea != null ? yearTotal2ByArea.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", monthTotal2ByArea != null ? monthTotal2ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal2ByArea != null ? monthTotal2ByArea.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //申请计划
        Dto yearTotal3ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal3",query003Dto);//年度
        Dto monthTotal3ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal3",query003Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS",AREAS);
        lDto.put("PROJECT", "申请计划");
        lDto.put("YEAR_SALES_SUM", yearTotal3ByArea != null ? yearTotal3ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal3ByArea != null ? yearTotal3ByArea.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", monthTotal3ByArea != null ? monthTotal3ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal3ByArea != null ? monthTotal3ByArea.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //实际执行
        Dto yearTotal4ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal4",query003Dto);//年度
        Dto monthTotal4ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal4",query003Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS",AREAS);
        lDto.put("PROJECT", "实际执行");
        lDto.put("YEAR_SALES_SUM", yearTotal4ByArea != null ? yearTotal4ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal4ByArea != null ? yearTotal4ByArea.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", monthTotal4ByArea != null ? monthTotal4ByArea.getBigDecimal("SALES_SUM") : "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal4ByArea != null ? monthTotal4ByArea.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //可用额度
        Dto yearTotal5ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal5",query003Dto);//年度
        Dto monthTotal5ByArea = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal5",query003Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS",AREAS);
        lDto.put("PROJECT", "可用额度");
        lDto.put("YEAR_SALES_SUM", "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal5ByArea != null ? yearTotal5ByArea.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal5ByArea != null ? monthTotal5ByArea.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //达成率/超额率%
        //达成率=（实际执行/月累计预算）*100%
        //费用超额率=（“实际执行”-“可用额度”）/“可用额度”*100%
        lDto = Dtos.newDto();
        lDto.put("AREAS",AREAS);
        lDto.put("PROJECT", "达成率/超额率%");
        if(yearTotal2ByArea.getBigDecimal("SALES_SUM").compareTo(BigDecimal.ZERO) != 0){//达成率
            BigDecimal YEAR_SALES_SUM_AREA = yearTotal4ByArea.getBigDecimal("SALES_SUM").multiply(new BigDecimal(100)).divide(yearTotal2ByArea.getBigDecimal("SALES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("YEAR_SALES_SUM", YEAR_SALES_SUM_AREA+"%");
        }else {
            lDto.put("YEAR_SALES_SUM", "0%");
        }
        if(yearTotal5ByArea.getBigDecimal("EXPENSES_SUM").compareTo(BigDecimal.ZERO) != 0){//费用超额率
            BigDecimal YEAR_EXPENSES_SUM_AREA = (yearTotal4ByArea.getBigDecimal("EXPENSES_SUM").subtract(yearTotal5ByArea.getBigDecimal("EXPENSES_SUM"))).multiply(new BigDecimal(100)).divide(yearTotal5ByArea.getBigDecimal("EXPENSES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("YEAR_EXPENSES_SUM", YEAR_EXPENSES_SUM_AREA+"%");
        }else {
            lDto.put("YEAR_EXPENSES_SUM", "0%");
        }
        if(monthTotal2ByArea.getBigDecimal("SALES_SUM").compareTo(BigDecimal.ZERO) != 0){
            BigDecimal MONTH_SALES_SUM_AREA = monthTotal4ByArea.getBigDecimal("SALES_SUM").multiply(new BigDecimal(100)).divide(monthTotal2ByArea.getBigDecimal("SALES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("MONTH_SALES_SUM", MONTH_SALES_SUM_AREA+"%");
        }else {
            lDto.put("MONTH_SALES_SUM", "0%");
        }
        if(monthTotal5ByArea.getBigDecimal("EXPENSES_SUM").compareTo(BigDecimal.ZERO) != 0){
            BigDecimal MONTH_EXPENSES_SUM_AREA = (monthTotal4ByArea.getBigDecimal("EXPENSES_SUM").subtract(monthTotal5ByArea.getBigDecimal("EXPENSES_SUM"))).multiply(new BigDecimal(100)).divide(monthTotal5ByArea.getBigDecimal("EXPENSES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("MONTH_EXPENSES_SUM", MONTH_EXPENSES_SUM_AREA+"%");
        }else {
            lDto.put("MONTH_EXPENSES_SUM", "0%");
        }
        result.add(lDto);

        //客户相关数据查询
        Dto query004Dto = Dtos.newDto();
        query004Dto.put("GWFE004LIST", gwfe004List);
        query004Dto.put("ENT", shiroUser.getDBC());
        query004Dto.put("YEAR", GWFE001);
        query004Dto.put("MONTH", GWFE002);
        lDto = Dtos.newDto();
        //年度总预算
        Dto yearTotal1ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal1",query004Dto); //年度
        lDto.put("AREAS","客户");
        lDto.put("PROJECT", "年度总预算");
        lDto.put("YEAR_SALES_SUM", yearTotal1ByCus != null ? yearTotal1ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal1ByCus != null ? yearTotal1ByCus.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", "");
        lDto.put("MONTH_EXPENSES_SUM", "");
        result.add(lDto);
        //月累计预算
        Dto yearTotal2ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal2",query004Dto);//年度
        Dto monthTotal2ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal2",query004Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS","客户");
        lDto.put("PROJECT", "月累计预算");
        lDto.put("YEAR_SALES_SUM", yearTotal2ByCus != null ? yearTotal2ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal2ByCus != null ? yearTotal2ByCus.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", monthTotal2ByCus != null ? monthTotal2ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal2ByCus != null ? monthTotal2ByCus.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //申请计划
        Dto yearTotal3ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal3",query004Dto);//年度
        Dto monthTotal3ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal3",query004Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS","客户");
        lDto.put("PROJECT", "申请计划");
        lDto.put("YEAR_SALES_SUM", yearTotal3ByCus != null ? yearTotal3ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal3ByCus != null ? yearTotal3ByCus.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", monthTotal3ByCus != null ? monthTotal3ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal3ByCus != null ? monthTotal3ByCus.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //实际执行
        Dto yearTotal4ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal4",query004Dto);//年度
        Dto monthTotal4ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal4",query004Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS","客户");
        lDto.put("PROJECT", "实际执行");
        lDto.put("YEAR_SALES_SUM", yearTotal4ByCus != null ? yearTotal4ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal4ByCus != null ? yearTotal4ByCus.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", monthTotal4ByCus != null ? monthTotal4ByCus.getBigDecimal("SALES_SUM") : "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal4ByCus != null ? monthTotal4ByCus.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //可用额度
        Dto yearTotal5ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getYearTotal5",query004Dto);//年度
        Dto monthTotal5ByCus = InvokeUtil.invokeBeanMethod("gwfeTMapper,getMonthTotal5",query004Dto);//月份
        lDto = Dtos.newDto();
        lDto.put("AREAS","客户");
        lDto.put("PROJECT", "可用额度");
        lDto.put("YEAR_SALES_SUM", "");
        lDto.put("YEAR_EXPENSES_SUM", yearTotal5ByCus != null ? yearTotal5ByCus.getBigDecimal("EXPENSES_SUM") : "");
        lDto.put("MONTH_SALES_SUM", "");
        lDto.put("MONTH_EXPENSES_SUM", monthTotal5ByCus != null ? monthTotal5ByCus.getBigDecimal("EXPENSES_SUM") : "");
        result.add(lDto);
        //达成率/超额率%
        //达成率=（实际执行/月累计预算）*100%
        //费用超额率=（“实际执行”-“可用额度”）/“可用额度”*100%
        lDto = Dtos.newDto();
        lDto.put("AREAS","客户");
        lDto.put("PROJECT", "达成率/超额率%");
        if(yearTotal2ByCus.getBigDecimal("SALES_SUM").compareTo(BigDecimal.ZERO) != 0){
            BigDecimal YEAR_SALES_SUM_CUS = yearTotal4ByCus.getBigDecimal("SALES_SUM").multiply(new BigDecimal(100)).divide(yearTotal2ByCus.getBigDecimal("SALES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("YEAR_SALES_SUM", YEAR_SALES_SUM_CUS+"%");
        }else {
            lDto.put("YEAR_SALES_SUM", "0%");
        }
        if(yearTotal5ByCus.getBigDecimal("EXPENSES_SUM").compareTo(BigDecimal.ZERO) != 0){
            BigDecimal YEAR_EXPENSES_SUM_CUS = (yearTotal4ByCus.getBigDecimal("EXPENSES_SUM").subtract(yearTotal5ByCus.getBigDecimal("EXPENSES_SUM"))).multiply(new BigDecimal(100)).divide(yearTotal5ByCus.getBigDecimal("EXPENSES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("YEAR_EXPENSES_SUM", YEAR_EXPENSES_SUM_CUS+"%");
        }else {
            lDto.put("YEAR_EXPENSES_SUM", "0%");
        }
        if(monthTotal2ByCus.getBigDecimal("SALES_SUM").compareTo(BigDecimal.ZERO) != 0){
            BigDecimal MONTH_SALES_SUM_CUS = monthTotal4ByCus.getBigDecimal("SALES_SUM").multiply(new BigDecimal(100)).divide(monthTotal2ByCus.getBigDecimal("SALES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("MONTH_SALES_SUM", MONTH_SALES_SUM_CUS+"%");
        }else {
            lDto.put("MONTH_SALES_SUM", "0%");
        }
        if(monthTotal5ByCus.getBigDecimal("EXPENSES_SUM").compareTo(BigDecimal.ZERO) != 0){
            BigDecimal MONTH_EXPENSES_SUM_CUS = (monthTotal4ByCus.getBigDecimal("EXPENSES_SUM").subtract(monthTotal5ByCus.getBigDecimal("EXPENSES_SUM"))).multiply(new BigDecimal(100)).divide(monthTotal5ByCus.getBigDecimal("EXPENSES_SUM"),2,ROUND_HALF_DOWN);
            lDto.put("MONTH_EXPENSES_SUM", MONTH_EXPENSES_SUM_CUS+"%");
        }else {
            lDto.put("MONTH_EXPENSES_SUM", "0%");
        }
        result.add(lDto);
        outDto.put("list", result);
        return outDto;
    }
}
